﻿using System.Text;

namespace wojilu
{
    /// <summary>
    /// StringBuilder 扩展
    /// </summary>
    public class MyStringBuilder
    {
        StringBuilder OutPut;

        public MyStringBuilder()
        {
            OutPut = new StringBuilder();
        }
        /// <summary>
        /// 多少个Tab缩进
        /// </summary>
        /// <param name="KongGe">空格</param>
        /// <returns></returns>
        static public string Tab(int KongGe)
        {
            return new string('\t', KongGe);
        }
        /// <summary>
        /// 转义类型
        /// </summary>
        public enum ESC
        {
            Js, String
        }
        //以字符串的形式
        public string AppendLine(string Text)
        {
            return AppendLine(Text, ESC.Js);
        }
        public void AppendLine(int TabNum, string Text)
        {
            this.OutPut.Append(Tab(TabNum));
            this.OutPut.Append(Text);
            this.AppendLine("", ESC.Js);
        }
        public string AppendLine(string Text, ESC e)
        {
            if (e == ESC.Js)
            {
                this.OutPut.Append(Text + "\r\n");
            }
            else
            {
                this.OutPut.Append(Text + "\\r\\n");
            }
            return OutPut.ToString();
        }

        public void Append(string Text)
        {
            this.OutPut.Append(Text);
        }

        public void AppendFormat(string Text, params object[] args)
        {
            this.OutPut.AppendFormat(Text, args);
        }

        public void Replace(string OldText, string NewText)
        {
            this.OutPut.Replace(OldText, NewText);
        }



        #region 剔除最后一个标点符号
        /// <summary>
        /// 剔除最后一个逗号
        /// </summary>
        public void TrimEnd()
        {
            TrimEnd(",");
        }
        /// <summary>
        /// 剔除最后一个标点符号
        /// </summary>
        /// <param name="c"></param>
        public void TrimEnd(char c)
        {
            string str = this.OutPut.ToString();
            this.OutPut = new StringBuilder();
            try
            {
                this.OutPut.Append(str.Substring(0, str.LastIndexOf(c)));
            }
            catch
            {
                this.OutPut.Append(str);
            }
        }
        /// <summary>
        /// 剔除最后字符
        /// </summary>
        /// <param name="c"></param>
        public void TrimEnd(string c)
        {
            string str = this.OutPut.ToString();
            this.OutPut = new StringBuilder();
            try
            {

                this.OutPut.Append(str.Substring(0, str.LastIndexOf(c)));
            }
            catch
            {
                this.OutPut.Append(str);
            }
        }
        #endregion

        public override string ToString()
        {
            if (OutPut == null)
                return "";
            else
                return OutPut.ToString();
        }
    }
}
